package cn.fintecher.pangolin.service.business.service;

import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.enums.MessageMode;
import cn.fintecher.pangolin.common.enums.MessageType;
import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.DepartmentModel;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.model.WebSocketMessageModel;
import cn.fintecher.pangolin.common.response.LoginResponse;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.CaseTransferLog;
import cn.fintecher.pangolin.service.business.client.WebSocketClient;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class BaseService {

    @Autowired
    private RedisTemplate<String, Object> jsonRedisTemplate;

    @Autowired
    private WebSocketClient webSocketClient;

    /**
     * @Author wangxiangdong
     * @Description 根据token获取用户信息
     * @Date 2019/9/2 15:40
     **/
    public UserModel getUserByToken(String token) {
        LoginResponse response = (LoginResponse) jsonRedisTemplate.opsForValue().get(token);
        if (Objects.isNull(response)) {
            throw new BadRequestException(null, "user", "token.is.error");
        }
        return response.getUserModel();
    }

    public DepartmentModel getDepartmentByToken(String token) {
        LoginResponse response = (LoginResponse) jsonRedisTemplate.opsForValue().get(token);
        if (Objects.isNull(response)) {
            throw new BadRequestException(null, "user", "token.is.error");
        }
        return response.getDepartmentModel();
    }

    public LoginResponse getUserSessionByToken(String token) {
        LoginResponse response = (LoginResponse) jsonRedisTemplate.opsForValue().get(token);
        if (Objects.isNull(response)) {
            throw new BadRequestException(null, "user", "token.is.error");
        }
        return response;
    }

    public void sendMessageToOne(MessageType type, String title, String content, String userId) {
        try {
            WebSocketMessageModel webSocketMessageModel = new WebSocketMessageModel();
            webSocketMessageModel.setMessageType(type);
            webSocketMessageModel.setTitle(title);
            webSocketMessageModel.setContent(content);
            webSocketMessageModel.setMsgDate(ZWDateUtil.getNowDateTime());
            webSocketMessageModel.setMessageMode(MessageMode.POPUP);
            this.webSocketClient.sendMsgByUserId(webSocketMessageModel, userId);
        } catch (Exception var6) {
            log.error(var6.getMessage(), var6);
        }
    }

    void sendMessageToMany(MessageType type, String title, String content, List<String> userIds) {
        try {
            WebSocketMessageModel webSocketMessageModel = new WebSocketMessageModel();
            webSocketMessageModel.setMessageType(type);
            webSocketMessageModel.setTitle(title);
            webSocketMessageModel.setContent(content);
            webSocketMessageModel.setMsgDate(ZWDateUtil.getNowDateTime());
            webSocketMessageModel.setMessageMode(MessageMode.POPUP);
            userIds.forEach((userId) -> {
                this.webSocketClient.sendMsgByUserId(webSocketMessageModel, userId);
            });
        } catch (Exception var6) {
            log.error(var6.getMessage(), var6);
        }

    }

    /**
     * @Author wangxiangdong
     * @Description 添加流转记录
     * @Date 2019/9/4 10:07
     **/
    CaseTransferLog createCaseTranferLog(String caseId, String operContent, UserModel userModel, String outUserId, String inUserId,double leftAmt) {
        CaseTransferLog caseTransferLog = new CaseTransferLog();
        caseTransferLog.setCaseId(caseId);
        caseTransferLog.setOperContent(operContent);
        caseTransferLog.setOutUserId(outUserId);
        caseTransferLog.setLeftAmt(leftAmt);
        caseTransferLog.setInUserId(inUserId);
        caseTransferLog.setUserName(userModel.getUsername());
        caseTransferLog.setFullName(userModel.getRealName());
        caseTransferLog.setOperatorTime(ZWDateUtil.getNowDateTime());
        return caseTransferLog;
    }

    /**
     * 拼接es合规过滤条件
     */
    public void spliceBuilder(BoolQueryBuilder builder, UserModel userModel) {
        if (userModel.getIsCheck().equals(BaseSelect.YES)) {
            builder.must(QueryBuilders.termsQuery("principalId.keyword", userModel.getPrincipalIds()));
        }
    }
}
